﻿using System;

using DevExpress.XtraBars;
using DevExpress.XtraEditors;

namespace ContractManager.UserControls.Controls
{
    public partial class BaseUserControl : XtraUserControl
    {
        #region Constants and Fields

        /// <summary>
        /// The main manager.
        /// </summary>
        private BarManager mainManager;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseUserControl"/> class.    Default constructor. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 30.11.2010. 
        /// </remarks>
        public BaseUserControl()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>   Gets the manager for this. </summary>
        /// <value> this manager. </value>
        protected virtual BarManager ThisManager
        {
            get
            {
                return null;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Register bar. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 25.11.2010. 
        /// </remarks>
        /// <param name="barManager">
        /// Manager for bar. 
        /// </param>
        public void RegisterBar(BarManager barManager)
        {
            this.mainManager = barManager;
        }

        #endregion

        #region Methods

        /// <summary>   Raises the load event. </summary>
        /// <remarks>   Golovkov Alex, 30.11.2010. </remarks>
        /// <param name="e">    Event information to send to registered event handlers. </param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.DesignMode)
            {
                return;
            }

            if (this.mainManager == null)
            {
                return;
            }

            this.mainManager.ForceInitialize();
            this.MergeToolbars(true);
        }

        /// <summary>
        /// Dispose of this object, cleaning up any resources it uses. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 25.11.2010. 
        /// </remarks>
        /// <param name="disposing">
        /// true if resources should be disposed, false if not. 
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.mainManager != null)
                {
                    this.MergeToolbars(false);
                }

                if (this.components != null)
                {
                    this.components.Dispose();
                }
            }

            base.Dispose(disposing);
        }

        /// <summary>
        /// The merge toolbars.
        /// </summary>
        /// <param name="merge">
        /// The merge.
        /// </param>
        private void MergeToolbars(bool merge)
        {
            foreach (Bar bar in this.ThisManager.Bars)
            {
                var mainBar = this.mainManager.Bars[bar.BarName];
                if (mainBar == null)
                {
                    continue;
                }

                if (merge)
                {
                    mainBar.Merge(bar);
                    bar.Visible = false;
                }
                else
                {
                    mainBar.UnMerge();
                }
            }
        }

        #endregion
    }
}
